/*
 * Baddie.cpp
 *
 *  Created on: Mar 2, 2010
 *      Author: dcrown
 */


#include "Baddie.h"
#include "..\Graphics\DXManager.h"
#include "..\Systems\Logger.h"
#include "..\Systems\BasicWindow.h"
#include "..\Systems\Clock.h"
#include "..\Systems\ResourceManager.h"

extern DXManager* gpDX;
extern BasicWindow* gpBW;
extern ResourceManager* gpRM;

const int IMG_WIDTH = 64;
const int IMG_HEIGHT = 64;
const int IMG_LEFT_OFFSET = 14;
const int IMG_RIGHT_OFFSET = 14;
const int IMG_TOP_OFFSET = 14;
const int IMG_BOTTOM_OFFSET = 14;

const float SPEED = 100.0;//pixels per second
const int MOVE_ANGLE_MAX = 30;

void Baddie::Create()
{
	windowWidth = gpBW->GetWidth();
	windowHeight = gpBW->GetHeight();

	texture = new Texture;
	texture->Create("textures\\Baddie.PNG", "baddie", IMG_WIDTH, IMG_HEIGHT);

	sprite = new Sprite;
	sprite->Create("baddie", texture);
	sprite->ScaleOriginal(0.5, 0.5);

	//create the baddie move pattern
	MovePattern* mp = gpRM->ReadMovePattern("movePatterns\\test.mp");

	pos.Clear();
	pos.Create(mp, IMG_WIDTH, IMG_HEIGHT);

	Logger::getInstance().LOG(Logger::DEBUG, "Baddie Created");
}

void Baddie::Destroy()
{
	texture->Destroy();
	delete texture;
	texture = NULL;

	sprite->Destroy();
	delete sprite;
	sprite = NULL;
}

void Baddie::Render()
{
	if (visible)
	{
		sprite->Draw();
	}
}

void Baddie::Update()
{
	if (visible)
	{
		pos.Update();

		if ((pos.GetOffsetX()+IMG_WIDTH-IMG_RIGHT_OFFSET < 0) ||
			(pos.GetOffsetX()+IMG_LEFT_OFFSET > windowWidth) ||
			(pos.GetOffsetY()+IMG_HEIGHT-IMG_BOTTOM_OFFSET < 0) ||
			(pos.GetOffsetY()+IMG_TOP_OFFSET > windowHeight))
		{
			Deactivate();
		}
		else
		{
			sprite->TranslateOriginal(pos.GetOffsetX(), pos.GetOffsetY());
			sprite->Update();
		}
	}
}

void Baddie::Activate(int x, int y)
{
	Logger::getInstance().LOG(Logger::DEBUG, "Baddie Activated: (%d, %d)", x, y);
	pos.SetAllPositions(x, y);
	pos.Start();
	//Logger::getInstance().LOG(Logger::DEBUG, "Setting Speed to: %f", SPEED);
	visible = true;
}

void Baddie::Deactivate()
{
	Logger::getInstance().LOG(Logger::DEBUG, "Baddie Deactivated");
	//deactivate
	visible = false;
}
